package com.yiboshi.rehrs.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.concurrent.*;

@Slf4j
public class ThreadPoolUtils {

    public static OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
    public static int logicalCores = osBean.getAvailableProcessors();//cpu核心数量

    public static int  max = logicalCores*4;

    public final static ExecutorService newFixedThreadPool = new ThreadPoolExecutor(logicalCores*2,
            max, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(1024 * 6));


    public static <T> Future<T> submitCallback(Callable<T> callback) {
        Future<T> submit = newFixedThreadPool.submit(callback);
        return submit;
    }

    public static boolean submitTask( Runnable command) {
        try {
            newFixedThreadPool.execute(() -> {
                        try {
                            command.run();
                        } finally {
                        }

                    }
            );
            return true;
        } catch (Exception e) {
            log.error("submitTask fail,{}", e.getMessage());
        }
        return false;
    }


}
